﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Eugenhome.Atrium
{
    /// <summary>
    /// 
    /// </summary>
    public class _Login
    {
        /// <summary></summary>
        private const string PRIVATEKEYNAME = "SignContent";

        /// <summary></summary>
        private const string KEYPRIVATEKEY = "aadlskdjölj8a9sf$%$kjlöia890f()laksjd";


        /// <summary></summary>
        private static string SignFullFileName { get { return Path.Combine(_serverMapPath, "sign.txt"); } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serverMapPath"></param>
        /// <returns></returns>
        public static bool Assigned(string serverMapPath)
        {
            _serverMapPath = serverMapPath;
            return File.Exists(SignFullFileName);
        }

        /// <summary></summary>
        private static string _serverMapPath = "";

        /// <summary></summary>
        private static CCryptography _serviceCry = new CCryptography(KEYPRIVATEKEY);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="serverMapPath"></param>
        /// <returns></returns>
        public static bool CheckPrivateKey(string key, string serverMapPath)
        {
            if (key == null) return false;

            if (!Assigned(serverMapPath)) return false;

            if (serverMapPath.Trim().Equals(string.Empty))
            {
                _serverMapPath = serverMapPath;
            }

            string dekey = DecryptPrivateKey(key);
            dekey = (dekey == null) ? key : dekey;

            CCryptography cry = new CCryptography(dekey);

            return cry.FileCrypted(SignFullFileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newkey"></param>
        /// <param name="serverMapPath"></param>
        /// <returns></returns>
        public static bool SetNewPrivateKey(string newkey, string serverMapPath)
        {
            if (!Assigned(serverMapPath))
            {
                string content = "";

                Random rnd = new Random();
                for (int i = 0; i < 25; i++) content += rnd.Next(10, 20).ToString();

                try
                {
                    StreamWriter sw = File.CreateText(SignFullFileName);
                    sw.Write(content);
                    sw.Close();

                }
                catch { return false; }
            }

            CCryptography cry = new CCryptography(newkey);

            cry.FileEncrypt(SignFullFileName);

            return true;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string EncryptPrivateKey(string key)
        {
            return _serviceCry.Encrypt64(key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DecryptPrivateKey(string key)
        {
            return _serviceCry.Decrypt64(key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool DecryptFile(string filename, string key)
        {
            CCryptography cry = new CCryptography(DecryptPrivateKey(key));

            return cry.FileDecrypt(filename);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool EncryptFile(string filename, string key)
        {
            CCryptography cry = new CCryptography(DecryptPrivateKey(key));

            return cry.FileEncrypt(filename);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DecryptContent(string filename, string key)
        {
            CCryptography cry = new CCryptography(DecryptPrivateKey(key));

            string result="";
            try
            {
                byte[] daten = File.ReadAllBytes(filename);

                byte[] daten2 = new byte[0];

                daten2 = cry.Decrypt(daten);

                result = Encoding.ASCII.GetString(daten2);
            }
            catch { }

            return result;
        }
    }
}
